package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.BankCardResponse;
import com.jz.jd.rest.response.BaseResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;
import jz.com.feierman.R;

/**
 * Created by Administrator on 2018/3/8.
 */

public class MyBankActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;



    MyAdapter myAdapter = new MyAdapter();
    List<BankCardResponse.BankCard> list = new ArrayList<>();


    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 10;
            }
        });
    }

    @Override
    protected void initData() {
//        getAllAddress();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_my_bank;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_bank, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            holder.tv_card_number.setText(list.get(position).bank_num);
            holder.tv_bank_name.setText(list.get(position).bank_name);
            holder.tv_user_name.setText(list.get(position).bank_holder);
            holder.tv_card_type.setText(list.get(position).bank_type);
            holder.btn_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("obj",list.get(position));
                    startActivity(AddBankActivity.class,bundle);
                }
            });
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delBankCard();
                }
            });
        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_card_number, tv_bank_name, tv_user_name,tv_card_type;
            CheckBox check_address;
            LinearLayout btn_edit,btn_del;

            public Holder(View itemView) {
                super(itemView);
                check_address = itemView.findViewById(R.id.check_address);
                tv_card_number = itemView.findViewById(R.id.tv_card_number);
                tv_bank_name = itemView.findViewById(R.id.tv_bank_name);
                tv_user_name = itemView.findViewById(R.id.tv_user_name);
                tv_card_type = itemView.findViewById(R.id.tv_card_type);
                btn_edit = itemView.findViewById(R.id.btn_edit);
                btn_del = itemView.findViewById(R.id.btn_del);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankCardInfo();
    }

    private void getBankCardInfo() {
        RestProxy.getInstance().getBankCardInfo(PreferencesHelper.getInstance().getString(MyBankActivity.this,"token"), new Observer<BankCardResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BankCardResponse bankCardResponse) {
                list.clear();
                list.add(bankCardResponse.map);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.btn_back)
    void btn_back() {
        finish();
    }


    private void setDefaultAddress(String user_address_id){
        RestProxy.getInstance().setDefaultAddress(PreferencesHelper.getInstance().getString(MyBankActivity.this,"token"), user_address_id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {

            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);
            }}

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.btn_add)
    void btn_add(){
        startActivity(AddBankActivity.class);
    }

    private void delBankCard(){
        RestProxy.getInstance().delBankCard(PreferencesHelper.getInstance().getString(MyBankActivity.this,"token")
                , new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(MyBankActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                list.clear();
                myAdapter.notifyDataSetChanged();
//                getBankCardInfo();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);
            }}

            @Override
            public void onComplete() {

            }
        });
    }




}
